package com.lw.spring.javademo.register;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private static final Map<String, String> registeredUsers = new HashMap<>(); // 模拟数据库存储

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        synchronized (registeredUsers) {
            if (registeredUsers.containsKey(username)) {
                // 用户名已存在
                response.getWriter().println("<html><body><h3 style='color:red;'>用户名已存在，请选择其他用户名。</h3></body></html>");
            } else {
                // 注册成功
                registeredUsers.put(username, password);
                response.getWriter().println("<html><body><h3 style='color:green;'>注册成功！</h3></body></html>");
            }
        }
    }

    public static Map<String, String> getRegisteredUsers() {
        return registeredUsers;
    }
}